﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using System.Text;

namespace Sustainalytics.Caching.Tests
{
    [TestClass]
    public class AppRedisCacheTests : AppCacheTests
    {
        protected int DatabaseId { get; set; }
        protected AppRedisCache RedisCache { get; set; }



        [TestMethod]
        public virtual void Add2Set_Success()
        {
            AppCache.Add2Set("add2set", "1");
            AppCache.Add2Set("add2set", "2");
            AppCache.Add2Set("add2set", "3");

            var result = AppCache.GetSet("add2set");
            StringBuilder text = new StringBuilder();
            foreach (var s in result)
                text.Append(s);

            Assert.AreEqual(text.ToString(), "123");
        }

        [TestMethod]
        [ExpectedException(typeof(CacheItemKeyNullOrEmptyException))]
        public virtual void Add2Set_Value_KeyNull_Exception()
        {
            AppCache.Add2Set("add2set", null);
        }

        protected override IAppCache CreateAppCache()
        {
            this.DatabaseId = 10;

            var redisCache = new AppRedisCache(ConfigurationManager.AppSettings["RedisServer"], this.DatabaseId, 5000, true);
            this.RedisCache = redisCache;
            return redisCache;
        }

        protected override void ClearAppCache()
        {
            this.RedisCache.Clear(this.DatabaseId);
        }


        [TestInitialize]
        public override void TestInitialize()
        {
            base.TestInitialize();
        }

        [TestCleanup]
        public override void TestCleanup()
        {
            base.TestCleanup();
        }


        [TestMethod]
        [ExpectedException(typeof(CacheItemKeyNullOrEmptyException))]
        public override void Set_StringValue_KeyNull_Exception()
        {
            base.Set_StringValue_KeyNull_Exception();
        }

        [TestMethod]
        public override void Set_StringValue_ValueNull_Success()
        {
            base.Set_StringValue_ValueNull_Success();
        }

        [TestMethod]
        public override void Set_StringValue_Success()
        {
            base.Set_StringValue_Success();
        }

        [TestMethod]
        [ExpectedException(typeof(CacheItemKeyNullOrEmptyException))]
        public override void Set_IEnumerableValue_KeyNull_Exception()
        {
            base.Set_IEnumerableValue_KeyNull_Exception();
        }

        [TestMethod]
        public override void Set_IEnumerableValue_ValueNull_Success()
        {
            base.Set_IEnumerableValue_ValueNull_Success();
        }

        [TestMethod]
        public override void Set_IEnumerableValue_Success()
        {
            base.Set_IEnumerableValue_Success();
        }

        [TestMethod]
        [ExpectedException(typeof(CacheItemKeyNullOrEmptyException))]
        public override void GetString_KeyNull_Exception()
        {
            base.GetString_KeyNull_Exception();
        }

        [TestMethod]
        public override void GetString_Success()
        {
            base.GetString_Success();
        }

        [TestMethod]
        [ExpectedException(typeof(CacheItemKeyNullOrEmptyException))]
        public override void GetSet_KeyNull_Exception()
        {
            base.GetSet_KeyNull_Exception();
        }

        [TestMethod]
        public override void GetSet_Success()
        {
            base.GetSet_Success();
        }

        [TestMethod]
        [ExpectedException(typeof(CacheItemKeyNullOrEmptyException))]
        public override void Remove_KeyNull_Exception()
        {
            base.Remove_KeyNull_Exception();
        }

        [TestMethod]
        public override void Remove_Success()
        {
            base.Remove_Success();
        }

        [TestMethod]
        public override void Subscribe_Set()
        {
            base.Subscribe_Set();
        }

        [TestMethod]
        public override void Subscribe_Remove()
        {
            base.Subscribe_Remove();
        }
    }
}
